import request from '@/utils/request'

// IM相关API接口

/**
 * 获取系统用户列表（用于IM联系人）
 */
export function getUserListForIm(params?: any) {
  return request({
    url: '/system/user/list',
    method: 'get',
    params
  })
}

/**
 * 获取IM用户列表（无需权限验证）
 */
export function getImUserList() {
  return request({
    url: '/im/user/list',
    method: 'get'
  })
}

/**
 * 获取会话列表
 */
export function getConversationList() {
  return request({
    url: '/im/conversation/list',
    method: 'get'
  })
}

/**
 * 标记消息已读
 */
export function markMessagesAsRead(toUserId?: number, groupId?: string) {
  return request({
    url: '/im/message/mark-read',
    method: 'post',
    params: {
      toUserId,
      groupId
    }
  })
}

/**
 * 用户登录IM
 */
export function loginToIm() {
  return request({
    url: '/im/login',
    method: 'post'
  })
}

/**
 * 用户登出IM
 */
export function logoutFromIm() {
  return request({
    url: '/im/logout',
    method: 'post'
  })
}


/**
 * 获取群组列表
 */
export function getGroups() {
  return request({
    url: '/im/group/list',
    method: 'get'
  })
}

/**
 * 发送私聊消息
 */
export function sendMessage(data: any) {
  return request({
    url: '/im/message/send',
    method: 'post',
    data
  })
}

/**
 * 发送群组消息
 */
export function sendGroupMessage(data: any) {
  return request({
    url: '/im/message/send-group',
    method: 'post',
    data
  })
}

/**
 * 获取消息历史
 */
export function getMessageHistory(params: any) {
  return request({
    url: '/im/message/history',
    method: 'get',
    params
  })
}

// ==================== 群聊相关API ====================

/**
 * 创建群聊
 */
export function createGroup(data: any) {
  return request({
    url: '/im/group/create',
    method: 'post',
    data
  })
}

/**
 * 获取群聊列表
 */
export function getGroupList() {
  return request({
    url: '/im/group/list',
    method: 'get'
  })
}

/**
 * 获取群聊详情
 */
export function getGroupInfo(groupId: string) {
  return request({
    url: `/im/group/${groupId}`,
    method: 'get'
  })
}

/**
 * 获取群成员列表
 */
export function getGroupMembers(groupId: string) {
  return request({
    url: `/im/group/${groupId}/members`,
    method: 'get'
  })
}

/**
 * 添加群成员
 */
export function addGroupMembers(groupId: string, data: any) {
  return request({
    url: `/im/group/${groupId}/members`,
    method: 'post',
    data
  })
}

/**
 * 移除群成员
 */
export function removeGroupMember(groupId: string, userId: number) {
  return request({
    url: `/im/group/${groupId}/members/${userId}`,
    method: 'delete'
  })
}

/**
 * 更新群信息
 */
export function updateGroupInfo(groupId: string, data: any) {
  return request({
    url: `/im/group/${groupId}`,
    method: 'put',
    data
  })
}

/**
 * 退出群聊
 */
export function quitGroup(groupId: string) {
  return request({
    url: `/im/group/${groupId}/quit`,
    method: 'post'
  })
}

/**
 * 解散群聊
 */
export function disbandGroup(groupId: string) {
  return request({
    url: `/im/group/${groupId}/disband`,
    method: 'post'
  })
}


/**
 * 获取用户IM信息
 */
export function getUserImInfo() {
  return request({
    url: '/im/user/info',
    method: 'get'
  })
}